var express = require('express');
var router = express.Router();
var db = require('../model/index')
const dateFn = require('../public/package')

/* 查询所有角色 */
router.get('/jurisdiction/getallrole', function (req, res, next) {
  let mysql = `SELECT t1.id,t1.name,t1.function,t1.addtime,t1.enable,t2.roleid,COUNT(*) AS num FROM role AS t1 LEFT JOIN admin AS t2 ON t1.id=t2.roleid GROUP BY t1.id;`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询角色失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询角色成功',
      data: data
    })
  })
});


/* 启动禁用角色 */
router.post('/jurisdiction/changeEnable', function (req, res, next) {
  let mysql = `UPDATE role SET enable='${req.body.enable}' WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '启用禁用角色失败，请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '启用禁用角色成功',
      data: data
    })
  })
});

/* 删除角色 */
router.post('/jurisdiction/delrole', function (req, res, next) {
  let mysql = `DELETE FROM role WHERE id = '${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除角色失败，请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '删除角色成功',
      data: data
    })
  })
});

/* 添加角色 */
router.post('/jurisdiction/addrole', function (req, res, next) {
  req.body.enable ? req.body.enable = '是' : req.body.enable = '否';
  req.body.time = dateFn();
  console.log(req.body.time)
  let mysql = `INSERT INTO role VALUES(NULL,'${req.body.name}','${req.body.function}','${req.body.time}','${req.body.enable}',NULL);`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '新增角色失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '新增角色成功',
    })
  })
});

/* 修改角色 */
router.post('/jurisdiction/editrole', function (req, res, next) {
  req.body.enable ? req.body.enable = '是' : req.body.enable = '否';
  let mysql = `UPDATE role SET ENABLE='${req.body.enable}',NAME='${req.body.name}',FUNCTION='${req.body.function}' WHERE id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '修改角色失败，请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '修改角色成功',
      data: data
    })
  })
});

/* 查询所有成员 */
router.get('/jurisdiction/getalladm', function (req, res, next) {
  console.log(req.query)
  let mysql = `SELECT t1.id,t1.username,t1.passwords,t1.name,t1.phone,t1.email,t1.roleid,t1.logintime,t1.enable,t2.name AS rolename FROM admin AS t1 LEFT JOIN role AS t2 ON t1.roleid=t2.id;`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询成员失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询成员成功',
      data: data
    })
  })
});


/* 启动禁用成员 */
router.post('/jurisdiction/admEnable', function (req, res, next) {
  let mysql = `UPDATE admin SET enable='${req.body.enable}' WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '启用禁用成员失败，请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '启用禁用成员成功',
      data: data
    })
  })
});

/* 删除成员 */
router.post('/jurisdiction/deladm', function (req, res, next) {
  let mysql = `DELETE FROM admin WHERE id = '${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除成员失败，请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '删除成员成功',
      data: data
    })
  })
});

/* 添加成员 */
router.post('/jurisdiction/addadm', function (req, res, next) {
  let mysql = `INSERT INTO admin VALUES(NULL,'${req.body.username}','${req.body.passwords}','${req.body.name}','${req.body.phone}','${req.body.email}','${req.body.roleid}',NULL,'是');`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '新增成员失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '新增成员成功',
    })
  })
});

/* 修改成员 */
router.post('/jurisdiction/editadm', function (req, res, next) {
  req.body.enable ? req.body.enable = '是' : req.body.enable = '否';
  let mysql = `UPDATE admin SET username='${req.body.username}',passwords='${req.body.passwords}',name='${req.body.name}',phone='${req.body.phone}',email='${req.body.email}',roleid='${req.body.roleid}',enable='${req.body.enable}' WHERE id=${req.body.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '修改角色失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '修改角色成功',
      data: data
    })
  })
});

/* 查询角色权限 */
router.get('/jurisdiction/menu', function (req, res, next) {
  let mysql = `SELECT * FROM role WHERE id=${req.query.id};`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询角色权限失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询角色权限成功',
      data: data
    })
  })
});

/* 修改角色权限 */
router.post('/jurisdiction/editmenu', function (req, res, next) {
  let mysql = `UPDATE role SET jurisdiction='${req.body.jurisdiction}' WHERE id='${req.body.id}'`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '修改角色权限失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '修改角色权限成功',
      data: data
    })
  })
});

/* 批量删除角色 */
router.post('/jurisdiction/batchdelrole', function (req, res, next) {
  let mysql = `DELETE FROM role WHERE `;
  for (let i = 0; i < req.body.length; i++) {
    mysql = mysql + `id=${req.body[i].id}` + ' or ';
  }
  mysql = mysql.replace(/or $/g, '');
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '批量删除角色失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '批量删除角色成功',
      data: data
    })
  })
});


module.exports = router;